$(function(){
	$("#companyWinId").window('close');
	$("#updateCompanyWinId").window('close');
	$('#dgDepartment').datagrid({    
	    url:'../../department/queryDepartment',
	    rownumbers:true,
	    singleSelect:true,
	    pagination:true,
	    method:'post',
	    queryParams: {
	    	type:'3',
	    	isDel:'1'
		},
		onClickRow: function (rowIndex, rowData) {
			$('#dg').datagrid('loadData',{total:0,rows:[]}); 
			$('#dgCompany').datagrid('loadData',{total:0,rows:[]}); 
            var id = rowData.id;
            $('#dg').datagrid({    
    		    url:'../creditCommitment/queryCreditCommitment',
    		    rownumbers:true,
    		    singleSelect:true,
    		    pagination:true,
    		    method:'post',
    		    queryParams: {
    		    	departmentCode:id,
    		    	isDel:'1'
    			},
    			onClickRow: function (rowIndex, rowData) {
    				$('#dgCompany').datagrid('loadData',{total:0,rows:[]}); 
    				var sysId = rowData.sysId;
    				$('#dgCompany').datagrid({    
    				    url:'../company/queryCompany',
    				    rownumbers:true,
    				    singleSelect:true,
    				    pagination:true,
    				    method:'post',
    				    queryParams: {
    				    	commitmentCode:sysId,
    				    	isDel:'1'
    					},
    				    toolbar:roleToo,
    				    columns:[[       
    				        {field:'companyName',title:'企业名称',width:'45%',align:'center'},
    				        {field:'companyCommitmentTime',title:'承诺时间',width:'45%',align:'center'/*,
    				        	formatter: function(value,row,index){
    				        	
    				        		var now = new Date(value);
    				    			var time= now.format("yyyy-MM-dd");
    				    			return time;
    							}*/
    				        } 
    				    ]]    
    				});
    				var p3 = $('#dgCompany').datagrid('getPager');

    	     	    p3.pagination({
    	     	        beforePageText: '',//页数文本框前显示的汉字 
    	     	        afterPageText: '',
    	     	        layout:['first','prev','list','next','last'],
    	     	        displayMsg: '第{from}至{to}条  共{total}条数据',
    	     	    });
    			},
    		    columns:[[       
    		        {field:'commitmentName',title:'承诺名称',width:'90%',align:'center'}/*,
    		        {field:'departmentName',title:'所属部门名称',width:'45%',align:'center'}*/
    		          
    		    ]]    
    		});
            var p2 = $('#dg').datagrid('getPager');

     	    p2.pagination({
     	        beforePageText: '',//页数文本框前显示的汉字 
     	        afterPageText: '',
     	        layout:['first','prev','list','next','last'],
     	        displayMsg: '第{from}至{to}条  共{total}条数据',
     	    });
        }, 
	    columns:[[       
	        {field:'departmentName',title:'部门名称',width:'96%',align:'center'}    
	          
	    ]]    
	});
	
	  var p1 = $('#dgDepartment').datagrid('getPager');

	   p1.pagination({
	        beforePageText: '',//页数文本框前显示的汉字 
	        afterPageText: '',
	        layout:['first','prev','list','next','last'],
	        displayMsg: '第{from}至{to}条  共{total}条数据',
	    });
	   
	   $('#dg').datagrid({    
		    url:'',
		    columns:[[       
		        {field:'commitmentName',title:'承诺名称',width:'90%',align:'center'}/*,
		        {field:'departmentName',title:'所属部门名称',width:'45%',align:'center'}*/
		          
		    ]]    
		});
	    /* 角色图标 */
	    var roleToo = [{
	    	text:'导入信息',
	    	iconCls:'icon-add',
	    	handler:function(){
	    		companyWin();
	    	}
	    },'-',{
	    	text:'修改',
	    	iconCls:'icon-edit',
	    	handler:function(){
	    		update();
	    	}
	    },'-',{
	    	text:'删除',
	    	iconCls:'icon-cut',
	    	handler:function(){
	    		delCompany();
	    	}
	    },'-',{
	    	text:'企业模板下载',
	    	iconCls:'icon-down',
	    	handler:function(){
	    		 window.location.href="../../excle/company.xlsx";
	    	}
	    }];
	    
	    $('#dgCompany').datagrid({    
		    url:'',
		   // toolbar:roleToo,
		    columns:[[       
		        {field:'companyName',title:'企业名称',width:'45%',align:'center'},
		        {field:'companyCommitmentTime',title:'承诺时间',width:'45%',align:'center',} 
		    ]]    
		});
})
//删除企业信息（逻辑删除）
function delCompany(){
	var dgCompany = $('#dgCompany').datagrid('getSelected');//企业信息
	if(dgCompany==null){
		$.messager.alert("提示","请选择企业信息","error");
	    return;
	}
	$.messager.confirm('确认','您确认想要删除记录吗？',function(r){    
	    if (r){    
	    	
	    	var sysId = dgCompany.sysId;
	    	$.ajax({  
	    		url:'../company/updateCompany',
	    		type:'post',
	    		data:{
	    		"sysId":sysId,
	    		"isDel":'2'
	    		},
	    		dataType:'json',
	    		error:function(){
	    			$.messager.alert("提示","操作异常","error");
	    		}, 
	    		success:function(data){
	    			var row = data.row;
	    			if(row>=0){
	    				$.messager.alert("提示","删除成功","info");
	    				$('#dgCompany').datagrid("reload");
	    				$("#updateCompanyWinId").window('close');
	    			}else{
	    				$.messager.alert("提示","删除失败","error");
	    			}
	    		}
	    })
	    }    
	}); 
	
}
//修改企业信息
function updateCompany(){
	var dgCompany = $('#dgCompany').datagrid('getSelected');//企业信息
	if(dgCompany==null){
		$.messager.alert("提示","请选择企业信息","error");
	    return;
	}
	var sysId = dgCompany.sysId;
	var companyName = $("#companyNameId").val().replace(/\s/g, "");
	var companyCommitmentTime = $('#companyCommitmentTimeId').datebox('getValue');
	
	if(companyName==''){
		$.messager.alert("提示","企业名称不能为空","error");
		return;
	}
	
	 $.ajax({  
			url:'../company/updateCompany',
			type:'post',
			data:{
			"sysId":sysId,
			"companyName":companyName,
			"companyCommitmentTime":companyCommitmentTime
			},
			dataType:'json',
			error:function(){
				$.messager.alert("提示","操作异常","error");
			}, 
			success:function(data){
				var row = data.row;
				if(row>=0){
					$.messager.alert("提示","修改成功","info");
					$('#dgCompany').datagrid("reload");
					$("#updateCompanyWinId").window('close');
				}else{
					$.messager.alert("提示","修改失败","error");
				}
			}
	})
}

function update(){
	var dgCompany = $('#dgCompany').datagrid('getSelected');//企业信息
	if(dgCompany==null){
		$.messager.alert("提示","请选择企业信息","error");
	    return;
	}
	var sysId = dgCompany.sysId;
	var companyName = dgCompany.companyName;//企业名称
	var companyCommitmentTime = dgCompany.companyCommitmentTime;//承诺时间
	
	$("#companyNameId").textbox('setValue',companyName);
	$('#companyCommitmentTimeId').datebox('setValue', companyCommitmentTime);	// 设置日期输入框的值

	$('#updateCompanyWinId').dialog({
		title: '修改企业信息',
		width: 330,
		height: 200,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				updateCompany();
			}
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#updateCompanyWinId').dialog('close');
			}
		}]
	});
}
//导入excle弹出窗
function companyWin(){
	var dgDepartment = $('#dgDepartment').datagrid('getSelected');//部门列表
     var dg = $('#dg').datagrid('getSelected');//承诺列表
     if(dgDepartment==null){
    	$.messager.alert("提示","请选择所属部门信息","error");
    	return;
     }
    if(dg==null){
    	$.messager.alert("提示","请选择承诺信息","error");
    	return;
     }
    
    $("#showId").show();
    $("#hideId").hide();
    $("#file_excle").after( $("#file_excle").clone().val(""));  
    $("#file_excle").remove();  
	$('#companyWinId').dialog({
		title: '导入企业信息',
		width: 330,
		height: 200,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'导入信息',
			iconCls:'icon-edit',
			handler:function(){
				var fileVal = $("#file_excle").val();
				if(fileVal ==''){
					alert("请先选择需要解析的EXCLE");
					return;
				}
				fn_excle_upload();
			}
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#companyWinId').dialog('close');
			}
		}]
	});
} 
var statsUpload='1';
//上传excle
function fn_excle_upload(){
	if(statsUpload=='2'){
		$.messager.alert("提示","正在导入，请稍等...","info");
		return;
	}
	statsUpload='2';
	$("#showId").hide();
    $("#hideId").show();
	
	  var url = '../upload/fileUpload';
	  var suffixStr = "xlsx,xls";
	  var savePath = ",them,fileload_excle";
	  $.ajaxFileUpload
    ({//String fileObjectId,int MaxSize,String suffixStr,String savePath,int width,int height
           url: url+'?fileObjectId=file_excle&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0, //用于文件上传的服务器端请求地址
           secureuri: false, //是否需要安全协议，一般设置为false
           fileElementId: 'file_excle', //文件上传域的ID
           dataType: 'json', //返回值类型 一般设置为json
           success: function (data, status)  //服务器成功响应处理函数
           {
               if(data.success=="true"){
             	     var excleUrl = data.imgurl;
             	     var dgDepartment = $('#dgDepartment').datagrid('getSelected');//部门列表
             	     var dg = $('#dg').datagrid('getSelected');//承诺列表
             	     if(dgDepartment==null){
             	    	$.messager.alert("提示","请选择所属部门信息","error");
             	    	return;
             	     }
             	    if(dg==null){
             	    	$.messager.alert("提示","请选择承诺信息","error");
             	    	return;
             	     }
             	    var commitmentCode = dg.sysId;
             	    var departmentCode = dgDepartment.id
             	    $.ajax({  
            			url:'../company/fileLoadExcle',
            			type:'post',
            			data:{
            			"url":excleUrl,
            			"commitmentCode":commitmentCode,
            			"departmentCode":departmentCode
            			},
            			dataType:'json',
            			error:function(){
            				$("#showId").show();
        		       	    $("#hideId").hide();
        		       	    statsUpload='1'
            				$.messager.alert("提示","操作异常","error");
            			}, 
            			success:function(data){
            				statsUpload='1';
            				var code = data.code;
            				if(code=='300'){
            					$.messager.alert("提示","请检查导入的模板是否正确","error");
            					$("#showId").show();
            		       	    $("#hideId").hide();
            					return;
            				}
            				var count = data.count;
            				if(count>=0){
            					$.messager.alert("提示","本次导入"+count+"条数据","info");
            					$('#dgCompany').datagrid("reload");
            					$('#companyWinId').dialog('close');
            					
            				}else{
            					$("#showId").show();
            		       	    $("#hideId").hide();
            					$.messager.alert("提示","导入数据失败","error");
            					
            				}
            			}
            		})
             	     
               }else{
            	    $("#showId").show();
            	    $("#hideId").hide();
            	    statsUpload='1'
             	    alert(data.msg);
               }
           },
           error: function (data, status, e)//服务器响应失败处理函数
           {
        	   $("#showId").show();
       	       $("#hideId").hide();
       	       statsUpload='1'
               alert(e);
           }
    })
}

Date.prototype.format = function(format){
			var o = {
			"M+" : this.getMonth()+1, //month
			"d+" : this.getDate(), //day
			"h+" : this.getHours(), //hour
			"m+" : this.getMinutes(), //minute
			"s+" : this.getSeconds(), //second
			"q+" : Math.floor((this.getMonth()+3)/3), //quarter
			"S" : this.getMilliseconds() //millisecond
			}

			if(/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
			}

			for(var k in o) {
			if(new RegExp("("+ k +")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
			}
			}
			return format;
		}